home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / examples / modeling / robot_arm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  3.1 KB  |  136 lines

  1. /*
  2.  * Copyright 1993, 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* robot_arm.c - draw a robot arm by composing modeling 
  19.  *    transformations
  20.  *
  21.  *    Escape Key    - exit program
  22.  */
  23. #include <GL/gl.h>
  24. #include <GL/glu.h>
  25. #include <GL/glut.h>
  26.  
  27. #include <stdio.h>
  28.  
  29. /*  Function Prototypes  */
  30.  
  31. GLvoid  initgfx( GLvoid );
  32. GLvoid  keyboard( GLubyte, GLint, GLint );
  33. GLvoid  drawScene( GLvoid );
  34.  
  35. void printHelp( char * );
  36.  
  37. /* Global Definitions */
  38.  
  39. #define KEY_ESC    27    /* ascii value for the escape key */
  40.  
  41. void
  42. main( int argc, char *argv[] )
  43. {
  44.     GLsizei width, height;
  45.  
  46.     glutInit( &argc, argv );
  47.  
  48.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  49.     height = glutGet( GLUT_SCREEN_HEIGHT );
  50.     glutInitWindowPosition( 0, height / 4 );
  51.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  52.     glutInitDisplayMode( GLUT_RGBA );
  53.     glutCreateWindow( argv[0] );
  54.  
  55.     initgfx();
  56.  
  57.     glutKeyboardFunc( keyboard );
  58.     glutDisplayFunc( drawScene ); 
  59.  
  60.     printHelp( argv[0] );
  61.  
  62.     glMatrixMode( GL_PROJECTION );
  63.     gluPerspective( 45.0, (GLdouble) width/ (GLdouble)height, 1.0, 20.0 );
  64.     glMatrixMode( GL_MODELVIEW );
  65.     
  66.     glutMainLoop();
  67. }
  68.  
  69. void
  70. printHelp( char *progname )
  71. {
  72.     fprintf(stdout, "\n%s - renders a robot arm\n\n"
  73.         "Escape Key        - exit the program\n\n",
  74.         progname);
  75. }
  76.  
  77. GLvoid
  78. initgfx( GLvoid )
  79. {
  80.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  81.     glShadeModel( GL_FLAT );
  82. }
  83.  
  84. GLvoid 
  85. keyboard( GLubyte key, GLint x, GLint y )
  86. {
  87.     switch (key) {
  88.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  89.         exit(0);
  90.     }
  91. }
  92.  
  93. GLvoid
  94. drawScene( GLvoid )
  95. {
  96.     static GLfloat    upperArmColor[] = { 1.0, 0.0, 0.0 };
  97.     static GLfloat    lowerArmColor[] = { 0.8, 0.5, 0.5 };
  98.     
  99.     glClear( GL_COLOR_BUFFER_BIT );
  100.  
  101.     glPushMatrix();
  102.         /* translate into the viewing volume */
  103.         glTranslatef( 0.0, 0.0, -8.0 );
  104.  
  105.         XYaxes();
  106.  
  107.         /* Draw the upper arm with the shoulder
  108.          * on the Z axis */
  109.  
  110.         /* Step 1: move to the center of the upper arm */
  111.         glTranslatef( 1.0, 0.0, 0.0 ); 
  112.  
  113.         /* Step 2: draw the upper arm */
  114.         glColor3fv( upperArmColor );
  115.         WireBox( 2.0, 0.4, 1.0 );
  116.  
  117.         /* Draw the lower arm at the end of the upper arm and 
  118.          * rotate it 45 degrees */
  119.  
  120.         /* Step 3: move to the elbow */
  121.         glTranslatef( 1.0, 0.0, 0.0 );
  122.  
  123.         /* Step 4: rotate the lower arm 45 degrees */
  124.         glRotatef( 45.0, 0.0, 0.0, 1.0 );
  125.  
  126.         /* Step 5: move to the center of the lower arm */
  127.         glTranslatef( 1.0, 0.0, 0.0 );
  128.  
  129.         /* Step 6: draw the lower arm */
  130.         glColor3fv( lowerArmColor );
  131.         WireBox( 2.0, 0.4, 1.0 );
  132.     glPopMatrix();
  133.  
  134.     glFlush();
  135. }
  136.